package yxy.game.pm2.command.plainsCraft;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsCraftMessage;
import yxy.game.pm2.module.plainsCraft.PlainsCraftHandle;
import yxy.game.pm2.module.plainsCraft.PlainsCraftPlayerModule;


/** 中原争霸 购买挑战次数 */
@MessageType(proto = Command.PlainsCraftPurchase, classtype = PlainsCraftMessage.PlainsCraftPurchaseRequest.class)
public class CMD_PlainsCraftPurchase extends Command<PlainsCraftMessage.PlainsCraftPurchaseRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsCraftMessage.PlainsCraftPurchaseRequest request) {
		String playerCode = messageSource.getTag();
		int times = request.getTimes();

		PlainsCraftMessage.PlainsCraftPurchaseResponse.Builder response = PlainsCraftMessage.PlainsCraftPurchaseResponse.newBuilder();

		if (times > 1000 || times <= 0) {
			send(messageSource, response, Result.ParamError);// 数据不对
			return;
		}

		PlainsCraftPlayerModule module = PlainsCraftPlayerModule.get(playerCode);
		if (module == null) {
			send(messageSource, response, Result.ModuleNotOpen);// 模块没有开启
			return;
		}

		Result result = new PlainsCraftHandle().buyTimes(module, times);
		response.setTimes(module.getTimes(module));
		response.setBuyPrice(module.getBuyTimesPrice(module));
		send(messageSource, response, result);
	}

	private void send(MessageSource messageSource, PlainsCraftMessage.PlainsCraftPurchaseResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}